<?php
/** Clasa pentru configuratia generala a aplicatiei;
 * @package AMFramework
 * @subpackage Core
 * @author Marcu Alexandru
 * @version 1.0 $Build 305b  
 */
defined('_INDEX') or die('Restricted');

class Amfw_Config extends Amfw_Object {
	private $_fw;
	private $_app;
	
	// Public ===========================================================================
	public function getApplications() {
		return $this->_app;
	}

	public function getLibraries() {
		return $this->_fw->libraries;
	}
	
	public function getRouting() {
		return $this->_fw->routing;		
	}

	public function getFwConstants( $cname = null ) {
		if(!isset( $cname )):
			return $this->_fw->constants;
		else: return $this->_fw->constants[$cname];
		endif;
	}
	
	// Protected ========================================================================
	protected function _initConfig() {
		// Citirea din fisierele Xml cu configuratia platformei
		$this->xml_parse(FRAMEWORK_CONFIG_XML);
		$this->xml_parse(APPLICATION_CONFIG_XML);
		// Extinderea configuratiei aplicatiilor cu setup specific (interior)
		foreach($this->_app as $app) {
			$this->expand( $app );
		}
	}
	
	// Private ==========================================================================
	private function xml_parse($xml_file) {
		$xml = simplexml_load_file(CONFIG_PATH . DS .$xml_file);
		
		// switch in functie de fisierul xml pe care il citim
		// se observa ca xml-urile au denumiri deja cunoscute 
		switch($xml_file) {
			case FRAMEWORK_CONFIG_XML:
				$_fw = new stdClass();
				$_fw->constants = array();
				foreach($xml->constants->const as $const) {
					$name = (String) $const['name'];
					$value = (String) $const['value'];
					$_fw->constants[ $name ] = $value;
				}

				$_fw->libraries = array();
				foreach ($xml->libraries->lib as $lib) 
				{
					$item = new stdClass();
					$item->name = (String) $lib['name'];
					$item->prefix = (String) $lib['prefix'];
					$item->path = (String) $lib['path'];
		
					$_fw->libraries[] = $item;	
				}

				
				$_fw->routing = array();
				$_fw->routing['enabled'] = (String) $xml->routing['enable'];
				
				$tokensArr = array();

				foreach($xml->routing->token as $token) {
					$order = (int) $token['order'];
					$var = (String) $token['var'];
					$default = (String) $token['default'];
					$tokensArr[$order][$var] = $default; 
				}
				$_fw->routing['tokens'] = $tokensArr;
				
				//
				//foreach($tokensArr as $order=> $default)
				//{
				//	list($var,$default) = each($default);
				//	echo $order .$var1  .$var2;
				//}
				//var_dump($_fw);
				
				$this->_fw = $_fw;
				return;
			case APPLICATION_CONFIG_XML:
				$_app = array();
				
				foreach($xml->app as $app) {
					$item = new stdClass();
					$item->name = (String) $app['name'];
					$item->prefix = (String) $app['prefix'];
					$item->path = (String) $app->path;
					$item->controller =(String) $app->handler;
					$item->setup =(String) $app->config;
					$item->setupFormat = (String) $app->config['format'];
					
					$_app[] = $item;
				}
				
				$this->_app = $_app;
				return;				
		}
	}
	
	private function expand( &$app ) {
		$loader_path = $this->realpath($app->path) . DS . $this->_fw->constants["Default_Application_EntryPoint_Name"] . '.php';
		$setup_path = $this->realpath($app->path) . DS . $app->setup . '.' . $app->setupFormat;
		
		// se observa ca denumirile nu sunt cunoscute, setarile de aplicatie fiind definite de dev
		if(file_exists($loader_path) && file_exists($setup_path)) {
			if($app->setupFormat == 'xml') {
				$xml = simplexml_load_file($setup_path);
				foreach($xml as $key => $item) {
					
					$vars = get_object_vars($item);
					if(!empty($vars["@attributes"])) {
						$tmp = array_merge($vars, $vars["@attributes"]);
						unset($tmp["@attributes"]);
					}
					else $tmp = $vars;
					foreach($tmp as &$tmp_el) {
						$tmp_el = (String) $tmp_el;
					}
					unset($tmp_el);
					$app->$key = $tmp;

				}
			}
			elseif($app->setupFormat == 'ini') {
				$ini = parse_ini_file($setup_path, true);
				foreach($ini as $section => $array) {
					$app->$section = $array;
				}
			}//~!if
		}//~!if(file...
	}
	
/*	private function expand( &$app ) {
		$loader_path = $this->realpath($app->path) . DS . $this->_fw->constants["Default_Application_EntryPoint_Name"] . '.php';
		$setup_path = $this->realpath($app->path) . DS . $app->setup . '.ini';

		if(file_exists($loader_path) && file_exists($setup_path)) {
			$ini = parse_ini_file($setup_path, true);
			foreach($ini as $section => $array) {
				$app->$section = $array;
			}
			return true;
		}
		return false;
	}*/
	
	private function realpath($path) {
		$tmp = str_replace('%Root', BASE_PATH, $path);
		$tmp = str_replace('%AppRoot', APP_PATH, $path);
		$tmp = str_replace('.', DS, $tmp);
		
		return $tmp;
	}
}